package org.deng.vertx.springboot.pojo;

import lombok.Data;

/**
 * @Author: DengLibin
 * @Date: Create in 2021-12-06 13:47:21
 * @Description:
 */
@Data
public class ResData<T> {
    //返回的基础数据
    private T data;
    //返回的状态码
    private int statusCode;
    //返回的提示语---给用户的
    private String statusMsg;


    private static final int STATE_CODE_OK = 200;

    public static <D> ResData<D> ok() {
        return ResDataBuilder(null, "", STATE_CODE_OK, null);
    }

    public static <D> ResData<D> ok(String statusMsg) {
        return ResDataBuilder(null, statusMsg, STATE_CODE_OK, null);
    }

    public static <D> ResData<D> ok(D data) {
        return ResDataBuilder(data, "请求响应成功", STATE_CODE_OK, null);
    }

    public static <D> ResData<D> okData(D data) {
        return ResDataBuilder(data, "请求响应成功", STATE_CODE_OK, null);
    }

    public static <D> ResData<D> ok(D data, String statusMsg) {
        return ResDataBuilder(data, statusMsg, STATE_CODE_OK, null);
    }

    public static ResData<Boolean> bool(Boolean res) {
        if (res == null || Boolean.FALSE.equals(res))
            return ResData.error("失败!");
        return ResData.ok();
    }

    public static ResData<Boolean> bool(Boolean res, String message) {
        if (res == null || Boolean.FALSE.equals(res))
            return ResData.error(message);
        return ResData.ok();
    }

    public static <D> ResData<D> error(String statusMsg, int statusCode) {
        return ResDataBuilder(null, statusMsg, statusCode, null);
    }

    public static <D> ResData<D> error(Exception e) {
        return ResDataBuilder(null, e.getMessage(), -1, null);
    }


    public static <D> ResData<D> error(String statusMsg) {
        return ResDataBuilder(null, statusMsg, -1, null);
    }

    public static <D> ResData<D> ResDataBuilder(D data, String statusMsg, int statusCode, Object errData) {
        ResData<D> ResData = new ResData<>();
        ResData.data = data;
        ResData.statusCode = statusCode;
        ResData.statusMsg = statusMsg;
        return ResData;
    }

    public static <D> ResData<D> sendWS(D data, int statusCode) {
        return ResDataBuilder(data, "", statusCode, null);
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }


    @Override
    public String toString() {
        return "ResData{" +
                "data=" + data +
                ", statusCode=" + statusCode +
                ", statusMsg='" + statusMsg + '\'' +
                '}';
    }

}
